//
//  MessageCell.m
//  11-QQ聊天
//
//  Created by wangneihan on 15/12/21.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "MessageCell.h"
#import "MessageFrame.h"
#import "Message.h"

@interface MessageCell()

@property (nonatomic , weak) UILabel *timeLabel;

@property (nonatomic , weak) UIButton *msgButton;

@property (nonatomic , weak) UIImageView *iconView;

@end

@implementation MessageCell

+(instancetype)initWithTableView:(UITableView *)tableView{
    static NSString *reuseId = @"msg";
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if(cell == nil)
    {
        cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //时间
        UILabel *timeLbl = [[UILabel alloc]init];
        [self.contentView addSubview:timeLbl];
        timeLbl.textAlignment = NSTextAlignmentCenter;
        self.timeLabel = timeLbl;
        //头像
        UIImageView *iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        //内容
        UIButton *msgBtn = [[UIButton alloc]init];
        [self.contentView addSubview:msgBtn];
        self.msgButton = msgBtn;
        //设置字体大小
        msgBtn.titleLabel.font = [UIFont systemFontOfSize:FontSize];
        //设置换行
        msgBtn.titleLabel.numberOfLines = 0;
        //设置字体的颜色
        [msgBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //设置按钮中内容的边距
        //msgBtn.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
        //msgBtn.backgroundColor = [UIColor redColor];
    }
    self.backgroundColor = [UIColor clearColor];
    return  self;
}

-(void)setMessageFrame:(MessageFrame *)messageFrame
{
    _messageFrame = messageFrame;
    Message *msg = messageFrame.message;
    
    self.timeLabel.text = msg.time;
    
    NSString *iconName = msg.type == MessageTypeSelf ? @"me": @"other";
    self.iconView.image = [UIImage imageNamed:iconName];
    //设置头像圆形
    self.iconView.layer.cornerRadius = 25;
    self.iconView.layer.masksToBounds = YES;
    
    
    [self.msgButton setTitle:msg.text forState:UIControlStateNormal];
    self.msgButton.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
    //设置消息的背景图片
    if (msg.type == MessageTypeSelf) {
        [self.msgButton setBackgroundImage:[self resizeImage:@"chat_send_nor"] forState:UIControlStateNormal];
        [self.msgButton setBackgroundImage:[self resizeImage:@"chat_send_press_pic"] forState:UIControlStateHighlighted];
        
    }else{
        
        [self.msgButton setBackgroundImage:[self resizeImage:@"chat_recive_nor"] forState:UIControlStateNormal];
        [self.msgButton setBackgroundImage:[self resizeImage:@"chat_recive_press_pic"] forState:UIControlStateHighlighted];
    }
    
    self.timeLabel.frame = messageFrame.timeLabelFrame;
    self.msgButton.frame = messageFrame.msgButtonFrame;
    self.iconView.frame = messageFrame.iconViewlFrame;
}

-(UIImage *)resizeImage:(NSString *)imgName
{
    UIImage *bgImage =  [UIImage imageNamed:imgName];
    //缩放图片
    bgImage = [bgImage stretchableImageWithLeftCapWidth:bgImage.size.width / 2 topCapHeight:bgImage.size.height / 2];
    return bgImage;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
